﻿using Newtonsoft.Json;
using NX5Web.Utils;
using SDKs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;

namespace NX5Web.Controllers
{
    public class DoubaoController : BaseApiController
    {
        public class DrawRequest
        {
            public String model { set; get; }
            public String prompt { set; get; }
            public String Size { set; get; }
            public Boolean watermark { set; get; }
        }

        public class DrawResponse
        {
            public String model { set; get; }

            private List<DrawUrl> _data = new List<DrawUrl>();

            public List<DrawUrl> data
            {
                set {  _data = value; }
                get { return _data; }
            }

            private ResponseToken _usage = new ResponseToken();
            public ResponseToken usage
            {
                set { _usage = value; }
                get { return _usage; }
            }
        }

        public class DrawUrl
        {
            public String url { set; get; }
        }

        public class ResponseToken
        {
            public Int32 generated_images { set; get; }
            public Int32 output_tokens { set; get; }
            public Int32 total_tokens { set; get; }
        }


        public class DoubaoDrawParms
        {
            public String prompt { set; get; }
        }


        public class AudioRequest
        {
            public String Text { set; get; }
        }

         
        public class AINormal
        {
            public String Text { set; get; }
        }
          

        [HttpPost]
        public async Task<HttpResponseMessage> AIText(AINormal normal)
        {    
            String Result = "";
            try
            {

                HttpResponseMessage result = null;
                var response = await AIDoubao.AIBase(normal.Text, false);  

                result = new HttpResponseMessage
                {
                    Content = new StringContent("{\"data\":\"" + response + "\"}",
                        Encoding.GetEncoding("UTF-8"), "application/json")
                }; 
                return result; 
            }
            catch (Exception e)
            {
                Console.WriteLine($"Exception: {e.Message}");
            }

            return new HttpResponseMessage
            {
                Content = new StringContent("{\"error\":\"调用失败\"}",
                   Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        [HttpPost]
        public async Task<HttpResponseMessage> AIAudio(AudioRequest msg)
        {

            String AudioUrl = await AIDoubao.AIAudio(msg.Text);

            return new HttpResponseMessage
            {
                Content = new StringContent("{\"Url\":\"" + AudioUrl + "\"}",
           Encoding.GetEncoding("UTF-8"), "application/json")
            };

        }

        [HttpPost]
        public async Task<HttpResponseMessage> AIDraw(DoubaoDrawParms msg)
        {
            String MediaUrl = await AIDoubao.AIDraw(msg.prompt);

            return new HttpResponseMessage
            {
                Content = new StringContent("{\"Url\":\"" + MediaUrl + "\"}",
           Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }
    }
}